package com.ysxsoft.wdg.ui.activity.home;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.event.CommentEvent;
import com.ysxsoft.wdg.bean.response.FaceToFaceRespsonse;
import com.ysxsoft.wdg.bean.response.JfResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.widget.TimeCount;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.Call;

import static com.ysxsoft.wdg.bean.event.CommentEvent.EventStatus.REGISTER_OK;

public class ShareFaceActivity extends BaseActivity {


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_code)
    TextView btnCode;
    @BindView(R.id.et_psw1)
    EditText etPsw1;
    @BindView(R.id.et_psw2)
    EditText etPsw2;
    private TimeCount timeCount;
    private String mobile="";
    private String code="";
    private String p1="";
    private String p2="";
    @Override
    protected int getLayoutId() {
        return R.layout.activity_share_face;
    }

    @Override
    protected void initData() {
        initStatusBar(topView,false);
        titleContent.setText("面对面注册");
        timeCount = new TimeCount(60000, 1000, btnCode);


    }

    @Override
    protected void setListener() {

    }


    @OnClick({R.id.title_finish, R.id.btn_code, R.id.btn_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.btn_code:
                mobile = etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(mobile)) {
                    ToastUtils.showToast("请输入手机号");
                    return;
                }
                if (mobile.length()!=11) {
                    ToastUtils.showToast("手机号码格式不正确");
                    return;
                }
                postCode();
                break;
            case R.id.btn_sure:
                mobile = etPhone.getText().toString().trim();
                p1 = etPsw1.getText().toString().trim();
                p2 = etPsw2.getText().toString().trim();
                code = etCode.getText().toString().trim();

                if (TextUtils.isEmpty(code) || TextUtils.isEmpty(mobile) || TextUtils.isEmpty(p1) || TextUtils.isEmpty(p2)) {
                    ToastUtils.showToast("输入信息不能为空...");
                    return;
                }
                if(!p1.equals(p2)){
                    ToastUtils.showToast("密码输入不一致!");
                    return;
                }
                postRegister();
                break;
        }
    }

    private void postRegister() {
        OkHttpUtils.post()
                .url(UrlHost.FACE_TO_FACE)
                .addParams("uid", SharePrefUtils.getUserId())//订单id
                .addParams("mobile",mobile )//
                .addParams("pass", p1)//订单id
                .addParams("repass",p2 )//订单id
                .addParams("code", code)//订单id
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    //面对面注册取消失败
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    FaceToFaceRespsonse bean = JsonUtils.parseByGson(response, FaceToFaceRespsonse.class);
                    if (bean == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(bean.getCode())) {
                        ToastUtils.showToast(bean.getMs());
                        finish();
                    } else {
                        ToastUtils.showToast(bean.getMs());
                    }
                } else {
                    //数据为空
                }
            }
        });
    }

    private void postCode() {
        timeCount.start();
        mApiHelper.getMsmCode(mobile, new Observer<BaseBean<String>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseBean<String> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    code = baseBean.getData();
                } else {
                    ToastUtils.showToast(baseBean.getMs());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
